package com.hrt.web.member.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.AddressBook;
import com.hrt.bean.HRTMember;
import com.hrt.bean.MailRecord;
import com.hrt.bean.SmsRecord;
import com.hrt.util.GetAgeUtil;
import com.hrt.util.MD5Tool;
import com.hrt.util.RamUtil;
import com.hrt.util.ReadFromFile;
import com.hrt.util.WebParams;
import com.hrt.web.addressBook.service.AddressBookService;
import com.hrt.web.mailRecord.service.MailRecordService;
import com.hrt.web.member.service.HRTMemberService;
import com.hrt.web.smsRecord.service.SmsRecordService;

@Controller
@RequestMapping("HRTWEB")
public class HRTMemberControllor extends BaseController<HRTMember> {

	@Autowired
	private HRTMemberService hrtMemberService;
	@Autowired
	private AddressBookService addressBookService;
	@Autowired
	private SmsRecordService smsRecordService;
	@Autowired
	private MailRecordService mailRecordService;

	/**
	 * @param member
	 * @param request
	 * @return 会员注册
	 */
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public Integer UserRegister(@ModelAttribute("userInfo") HRTMember member,
			HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		Integer registerTip = null;
		String email = member.getMail();
		String pwd = member.getPassword();
		String phone = member.getMobile();
		String mbrName = member.getMbrName();
		String sex = request.getParameter("sex");
		String borth = request.getParameter("myborth");
		Date registTime = new Date();
		/*
		 * String flag =null;//判断会员名是否已经被注册 HRTMember hrtMember =
		 * hrtMemberService.getMemberByName(member.getMbrName());
		 */
		// 根据生日计算年龄
		int age = GetAgeUtil.getAge(borth, GetAgeUtil.getStringDate());

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(borth);
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		String checkNum = request.getParameter("hrtMemberCheckout");
		HttpSession session = request.getSession();
		String randCheckCode = (String) session.getAttribute("randCheckCode");

		boolean checkNumB = randCheckCode.equalsIgnoreCase(checkNum);
		boolean usernameB = hrtMemberService.findMember(email);
		if (usernameB) {
			if (checkNumB) {
				// 邮箱验证码
				String jym = MD5Tool.MD5(RamUtil.getRandomPwd());
				HRTMember hu = new HRTMember(email, phone, pwd, "0", jym);
				hu.setMbrName(mbrName);
				hu.setSex(Integer.parseInt(sex));
				hu.setBorn(new java.sql.Date(date.getTime()));
				hu.setAge(age);
				hu.setRegisterTime(registTime);
				hrtMemberService.registerMember(hu);
				hrtMemberService.sendMail(email, jym);
				mv.addObject("hu", hu);
				registerTip = 0;
				// mv.setViewName("/HRTWEB/loginSuccess");
			} else {
				registerTip = 1;
				return registerTip;
				// mv.setViewName("/HRTWEB/register");
			}
		} else {
			/*
			 * mv.addObject("registerError", "用户名已存在");
			 * mv.setViewName("/HRTWEB/register");
			 */
			registerTip = 2;
		}
		return registerTip;
	}

	/* 用户注册四部曲 start */

	@RequestMapping(value = "/validateMobile", method = RequestMethod.POST)
	@ResponseBody
	public Map validateMobile(
			@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "name", required = true) String mobile,
			HttpServletRequest request) {

		Map<String, Object> map = new HashMap<String, Object>();
		
			HRTMember member = this.hrtMemberService.findMobile(param);
			if (member != null) {
				map.put("status", "n");
				map.put("info", "手机号码已经被使用");
			} else {
				map.put("status", "y");
				map.put("info", "手机号码验证通过");

			}
		return map;
	}

	/**
	 * 存储手机号码
	 * 
	 * @param mobile
	 * @param code
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/putMobile", method = RequestMethod.POST)
	@ResponseBody
	public Map putMobile(
			@RequestParam(value = "mobile", required = true) String mobile,
			@RequestParam(value = "code", required = true) String code,
			HttpServletRequest request) {
		Map result = new HashMap();
		Integer flag = null;
		HRTMember member = new HRTMember();
		member.setIsdelete(0);
		member.setMobile(mobile);
		SmsRecord smsRecord = smsRecordService.getCodeByMobile(mobile);
		if (smsRecord.getVaildCode().equals(code)) {

			this.hrtMemberService.saveMember(member);
			smsRecord.setIsinvalid(1);
			try {

				Long memberId = hrtMemberService.findMobile(mobile).getId();
				smsRecord.setMemberId(memberId);
				result.put("memberId", memberId);
				smsRecordService.updateSmsRecord(smsRecord);

			} catch (Exception e) {
				e.printStackTrace();
			}
			flag = 0;
		} else {
			flag = 1;
		}
		result.put("flag", flag);
		return result;

	}

	@RequestMapping(value = "/jumpToPersonalInfo")
	public ModelAndView jumpToPersonalInfo(
			@RequestParam(value = "memberId", required = true) Long memberId,
			HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("memberId", memberId);
		mv.setViewName("/HRTWEB/register/registerPersonalInfo");
		return mv;
	}

	/**
	 * 激活邮箱
	 * 
	 * @param mail
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/activeMail", method = RequestMethod.POST)
	@ResponseBody
	public String activeMail(
			@RequestParam(value = "mail", required = true) String mail,
			HttpServletRequest request) {
		if (mail != null) {

			// 邮箱验证码
			String jym = MD5Tool.MD5(RamUtil.getRandomPwd());

			hrtMemberService.sendMail(mail, jym);
		}
		return "success";
	}

	@RequestMapping(value = "/checkMemberName", method = RequestMethod.POST)
	@ResponseBody
	public Map checkMemberName(
			@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "name", required = true) String username,
			HttpServletRequest request) {

		Map<String, Object> map = new HashMap<String, Object>();
		
			HRTMember member = this.hrtMemberService.getMemberByName(param);
			if (member != null) {
				map.put("status", "n");
				map.put("info", "会员名已经被使用");
			} else {
				map.put("status", "y");
				map.put("info", "会员名验证通过");

			}
		
		return map;
	}

	@RequestMapping(value = "/checkMail", method = RequestMethod.POST)
	@ResponseBody
	public Map checkMail(
			@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "name", required = true) String mail,
			HttpServletRequest request) {

		Map<String, Object> map = new HashMap<String, Object>();
		
			HRTMember member = this.hrtMemberService.getByMail(param);
			if (member != null) {
				map.put("status", "n");
				map.put("info", "邮箱已经被使用");
			} else {
				map.put("status", "y");
				map.put("info", "邮箱验证通过");

			}
		
		return map;
	}

	/**
	 * 存储个人信息
	 * 
	 * @param member
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/putPersonalInfo", method = RequestMethod.POST)
	@ResponseBody
	public Map putPersonalInfo(
			@ModelAttribute("personalInfo") HRTMember member,
			HttpServletRequest request) {
		Map result = new HashMap();
		if (member != null && member.getId() != null) {

			HRTMember oldMember = hrtMemberService
					.getMemberByid(member.getId());
			oldMember.setAddress(member.getAddress());
			oldMember.setSex(member.getSex());

			oldMember.setMbrName(member.getMbrName());
			oldMember.setMail(member.getMail());
			String borth = request.getParameter("myborth");
			// 根据生日计算年龄
			int age = GetAgeUtil.getAge(borth, GetAgeUtil.getStringDate());

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = null;
			try {
				date = sdf.parse(borth);
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			oldMember.setAge(age);
			oldMember.setBorn(new java.sql.Date(date.getTime()));
			hrtMemberService.updateHRTMember(oldMember);
			result.put("memberId", member.getId());
		}

		return result;

	}

	@RequestMapping(value = "/jumpToPass")
	public ModelAndView jumpToPass(
			@RequestParam(value = "memberId", required = true) Long memberId,
			HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("memberId", memberId);
		mv.setViewName("/HRTWEB/register/registerPass");
		return mv;
	}

	/**
	 * 设置密码
	 * 
	 * @param memberId
	 * @param password
	 * @param pass
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/putPass", method = RequestMethod.POST)
	@ResponseBody
	public Integer putPass(
			@RequestParam(value = "id", required = true) Long memberId,
			@RequestParam(value = "password", required = true) String password,
			@RequestParam(value = "pass", required = true) String pass,
			HttpServletRequest request) {
		Integer flag = null;
		if (memberId != null) {
			HRTMember member = hrtMemberService.getMemberByid(memberId);
			if (password.equals(pass)) {
				String Md5Pass = MD5Tool.MD5(password);
				member.setPassword(Md5Pass);
				hrtMemberService.updateHRTMember(member);
				flag = 0;
			} else {
				flag = 1;
			}
		}
		return flag;

	}

	/* 用户注册四部曲 end */
	/**
	 * @param user
	 * @param request
	 * @return 注册成功后自动跳转到邮箱验证提示页面
	 */
	@RequestMapping(value = "/validateTip")
	public ModelAndView validateTip(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/HRTWEB/loginSuccess");
		return mv;
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public Integer UserLogin(@RequestParam(value = "loginName", required = true) String loginName,
			@RequestParam(value = "password", required = true) String password,
			@RequestParam(value = "hrtMemberCheckout", required = true) String hrtMemberCheckout,
			HttpServletRequest request, HttpServletResponse response) {
		Integer loginTip = null;
		HttpSession session = request.getSession();
		// 根据用户名密码登陆后返回一个HRTMember对象
		HRTMember hu = hrtMemberService.loginMember(loginName, password);
		// 验证码的值
		String randCheckCode = (String) session.getAttribute("randCheckCode");
		boolean b = randCheckCode.equalsIgnoreCase(hrtMemberCheckout);
		if (hu != null) {
			if (b) {

				this.setHRTMember(hu, request, response);
				Enumeration emus = session.getAttributeNames();
				while (emus.hasMoreElements()) {
					System.out.println(emus.nextElement().toString());
				}
				// session.setAttribute("HRTMember", hu);
				setHRTMember(hu, request, response);

				loginTip = 0;

			} else {
				loginTip = 2;
			}
		}else{
			loginTip=1;
		}
		return loginTip;
	}

	/**
	 * @param request
	 * @return 邮箱验证
	 */
	@RequestMapping(value = "mailCheckInfo")
	public ModelAndView mailCheckInfo(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		String toMail = request.getParameter("toMail");

		HRTMember hu = hrtMemberService.loginMember(toMail, null);

		String jym = request.getParameter("jym");
		String checkOut = hu.getMailCode();
		if (jym.equalsIgnoreCase(checkOut)) {
			hu.setIsMailVaild(1);
			hrtMemberService.updateMemberActivate(hu);
		}
		mv.setViewName("/HRTWEB/login");
		return mv;
	}

	/**
	 * @param request
	 * @return 修改密码，邮箱验证
	 */
	@RequestMapping(value = "mailCheckPass")
	public ModelAndView mailCheckPass(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("/HRTWEB/personalCenter/center");
		return mv;
	}

	@RequestMapping(value = "/check", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> checkUser(@RequestParam String uname) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean b = hrtMemberService.findMember(uname);
		map.put("checkValue", b);
		return map;
	}

	/**
	 * @param request
	 * @return 会员退出
	 */
	@RequestMapping(value = "/exit")
	public String exitUser(HttpServletRequest request) {
		HttpSession session = request.getSession();
		session.setMaxInactiveInterval(0);
		return "index";
	}

	/**
	 * @param request
	 * @return 个人中心页面
	 */
	@RequestMapping(value = "/center")
	public ModelAndView personCenter(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("/HRTWEB/personalCenter/center");
		return mv;
	}

	/**
	 * @param request
	 * @return 我的主页
	 */
	@RequestMapping(value = "/myCenter")
	public ModelAndView myCenter(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("/HRTWEB/personalCenter/myCenter");
		return mv;
	}

	/**
	 * @param request
	 * @return 我的主页
	 */
	@RequestMapping(value = "/integral")
	public ModelAndView integral(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("/HRTWEB/personalCenter/integral");
		return mv;
	}

	/**
	 * @param request
	 * @return 我的订单页面
	 */
	@RequestMapping(value = "/order")
	public ModelAndView personCenter1(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();

		mv.setViewName("/HRTWEB/myOrder");
		return mv;
	}

	/**
	 * @param reponse
	 * @param request
	 * @return 文件上传
	 */
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public ModelAndView receiveUpload(HttpServletResponse reponse,
			HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		HttpSession session = request.getSession();

		String name = request.getParameter("PhotoName");
		System.out.println("name::" + name);
		String fileName = "E://" + name;
		// 待存储进入数据库的图片名
		String photoName = ReadFromFile.readFileByBytes(fileName, request);
		HRTMember member = (HRTMember) session.getAttribute("HRTMember");
		member.setPic(photoName);
		hrtMemberService.updateHRTMember(member);
		System.out.println("上传成功");
		mv.setViewName("/HRTWEB/personalCenter/center");
		return mv;
	}

	/**
	 * @param reponse
	 * @param request
	 * @return
	 * @return 文件上传
	 */
	@RequestMapping(value = "/uploadtest", method = RequestMethod.POST)
	public ModelAndView receiveUploadtest(HttpServletResponse reponse,
			HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();

		List<String> fileTypes = WebParams.getFILE_TYPES();
		String name = request.getParameter("name");
		String type = request.getParameter("type");
		String size = request.getParameter("size");
		Long sizeNum = Long.parseLong(size);
		System.out.println("sizeNum" + sizeNum);

		if ((sizeNum >= 2024000) || fileTypes.contains(type)) {
			mv.setViewName("manage/member/error");
			return mv;
		} else {
			System.out.println("type::" + type);
			System.out.println("size::" + size);
			String str = null;
			try {

				str = new String(name.getBytes("8859_1"), "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}

			System.out.println("name::" + str);
			String fileName = "E://" + str;
			// 待存储进入数据库的图片名
			ReadFromFile.readFileByBytes(fileName, request);

			return null;
		}
	}

	/**
	 * 显示图片
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "/showImage")
	public ModelAndView showImage(
			@RequestParam(value = "saveImg", required = true) String imgUrl,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		FileInputStream hFile = new FileInputStream(imgUrl); // 以byte流的方式打开文件d:\1.gif
		int i = hFile.available(); // 得到文件大小
		byte data[] = new byte[i];
		hFile.read(data); // 读数据
		response.setContentType("image/*"); // 设置返回的文件类型
		OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
		toClient.write(data); // 输出数据

		toClient.flush();
		toClient.close();
		hFile.close();

		return null;
	}

	/**
	 * @param member
	 * @param request
	 * @return 修改个人信息
	 */
	@RequestMapping(value = "/reset")
	public ModelAndView resetMethod(
			@ModelAttribute("resetInfo") HRTMember member,
			HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		HttpSession session = request.getSession();
		// HRTMember oldMember = hrtMemberService.getMemberByid(member.getId());
		HRTMember oldMember = (HRTMember) session.getAttribute("HRTMember");

		oldMember.setMbrName(member.getMbrName());
		oldMember.setMail(member.getMail());
		oldMember.setMobile(member.getMobile());
		String borth = request.getParameter("myborth");

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(borth);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		oldMember.setBorn(new java.sql.Date(date.getTime()));

		// 根据生日计算年龄
		int age = GetAgeUtil.getAge(borth, GetAgeUtil.getStringDate());
		oldMember.setAge(age);
		if (member.getSequestion() != "") {
			oldMember.setSequestion(member.getSequestion());
		}
		if (member.getSeanswer() != "") {
			oldMember.setSeanswer(member.getSeanswer());
		}
		oldMember.setAddress(member.getAddress());
		if (member.getRealName() != "") {
			oldMember.setRealName(member.getRealName());
		}
		if (member.getNote() != "") {
			oldMember.setNote(member.getNote());
		}

		hrtMemberService.updateHRTMember(oldMember);
		mv.setViewName("/HRTWEB/personalCenter/reset");
		return mv;
	}

	/**
	 * @param member
	 * @param request
	 * @return 修改密码
	 */
	@RequestMapping(value = "/resetPass", method = RequestMethod.POST)
	@ResponseBody
	public Integer resetPass(@ModelAttribute("resetPassInfo") HRTMember member,
			@RequestParam(value = "code", required = true) String code,
			@RequestParam(value = "New_password", required = true) String New_password,
			@RequestParam(value = "validatePass", required = true) String validatePass,
			HttpServletRequest request) {
		HttpSession session = request.getSession();
		Integer flag = null;
		// 通过手机号码查找手机验证码
		SmsRecord smsRecord = smsRecordService.getCodeByMobile(member
				.getMobile());
		HRTMember Newmember = hrtMemberService.getMemberByid(member
				.getId());
		if (New_password.equals(validatePass)) {

			if (smsRecord.getVaildCode().equals(code)) {
				String MD5pass = MD5Tool.MD5(New_password);
				Newmember.setPassword(MD5pass);
				hrtMemberService.updateHRTMember(Newmember);
				session.removeAttribute("HRTMember");
				smsRecord.setIsinvalid(1);
				try {
					smsRecordService.updateSmsRecord(smsRecord);
				} catch (Exception e) {
					e.printStackTrace();
				}
				flag = 0;
			} else {
				flag = 1;
			}
		} else {
			flag = 2;
		}
		return flag;
	}

	/**
	 * @param request
	 * @return 验证密码
	 */
	@RequestMapping(value = "/validate", method = RequestMethod.POST)
	@ResponseBody
	public String validatePass(HttpServletRequest request) {
		String memberId = request.getParameter("id");
		String pass = request.getParameter("Old_password");
		String password = MD5Tool.MD5(pass);
		HRTMember member = this.hrtMemberService.getMemberByid(new Long(
				memberId));
		if (password.equals(member.getPassword())) {
			System.out.println("成功了");
			return "success";
		}
		System.out.println("失败了");
		return "error";
	}

	@RequestMapping(value = "/sentEmail")
	public ModelAndView returnPass(
			@RequestParam(value = "id", required = true) String id,
			HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		String email = request.getParameter("email");
		String pass = request.getParameter("password");
		String password = MD5Tool.MD5(pass);
		HRTMember member = this.hrtMemberService.getMemberByid(new Long(id));
		member.setPassword(password);
		hrtMemberService.updateHRTMember(member);
		hrtMemberService.sendMailPass(email, pass);
		mv.setViewName("/HRTWEB/personalCenter/center");
		return mv;
	}

	/**
	 * 地址管理
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/manageAddress")
	public ModelAndView manageAddress(
			@RequestParam(value = "memberId", required = true) Long memberId,
			HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		List<AddressBook> addressList = addressBookService
				.getAddListById(memberId);
		mv.addObject("addressList", addressList);
		mv.setViewName("/HRTWEB/personalCenter/manageAddress");
		return mv;
	}

	@RequestMapping(value = "/checkUser", method = RequestMethod.POST)
	@ResponseBody
	public Boolean checkUser(
			@RequestParam(value = "name", required = true) String username,
			@RequestParam(value = "param", required = true) String param,
			HttpServletRequest request) {
		return true;
	}

	/* 找回密码三部取 start */
	/**
	 * 找回密码------->验证用户名
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/checkName", method = RequestMethod.POST)
	@ResponseBody
	public Map checkName(
			@RequestParam(value = "username", required = true) String username,
			@RequestParam(value = "hrtMemberCheckout", required = true) String hrtMemberCheckout,
			HttpServletRequest request) {
		Map result = new HashMap();
		Integer flag = null;
		HttpSession session = request.getSession();
		String randCheckCode = (String) session.getAttribute("randCheckCode");
		boolean checkNumB = randCheckCode.equalsIgnoreCase(hrtMemberCheckout);
		try {
			HRTMember member = this.hrtMemberService.getMemberByName(username);
			if (member != null && checkNumB) {
				flag = 1;
				result.put("member", member);
			} else {
				flag = 3;
			}
		} catch (Exception e) {
			flag = 2;
			result.put("flag", flag);
			return result;
		}
		result.put("flag", flag);
		return result;
	}
	
	@RequestMapping(value = "/checkUsername", method = RequestMethod.POST)
	@ResponseBody
	public Map checkUsername(
			@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "name", required = true) String username,
			HttpServletRequest request) {

		Map<String, Object> map = new HashMap<String, Object>();
		
			HRTMember member = this.hrtMemberService.getMemberByName(param);
			if (member != null) {
				map.put("status", "y");
				map.put("info", "会员名验证通过");
			} else {
				map.put("status", "n");
				map.put("info", "会员名不存在");

			}
		
		return map;
	}

	/**
	 * 通过手机验证码修改密码
	 * 
	 * @param code
	 *            手机验证码
	 * @param mobile
	 *            手机号码
	 * @param password
	 *            需要重置的密码
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/changePass", method = RequestMethod.POST)
	@ResponseBody
	public Integer changePass(@ModelAttribute("confirmForm") HRTMember member,
			@RequestParam(value = "code", required = true) String code,
			@RequestParam(value = "pass", required = true) String pass,
			HttpServletRequest request) {
		Integer flag = null;
		// 通过手机号码查找手机验证码
		SmsRecord smsRecord = smsRecordService.getCodeByMobile(member
				.getMobile());
		if (member.getPassword().equals(pass)) {

			if (smsRecord.getVaildCode().equals(code)) {
				HRTMember Newmember = hrtMemberService.getMemberByid(member
						.getId());
				String MD5pass = MD5Tool.MD5(member.getPassword());
				Newmember.setPassword(MD5pass);
				hrtMemberService.updateHRTMember(Newmember);
				smsRecord.setIsinvalid(1);
				try {
					smsRecordService.updateSmsRecord(smsRecord);
				} catch (Exception e) {
					e.printStackTrace();
				}
				flag = 0;
			} else {
				flag = 1;
			}
		} else {
			flag = 2;
		}
		return flag;
	}

	/**
	 * 地址管理
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/jumpToConfirmUser")
	public ModelAndView jumpToConfirmUser(
			@RequestParam(value = "memberId", required = true) Long memberId,
			HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		HRTMember hrtMember = hrtMemberService.getMemberByid(memberId);
		String mobile = hrtMember.getMobile();
		mv.addObject("memberId", memberId);
		mv.addObject("mobile", mobile);
		mv.setViewName("/HRTWEB/forgetPass/confirmUser");
		return mv;
	}
	/* 找回密码三部取 end */
	/**
	 * 验证密码
	 * @param memberId
	 * @param param
	 * @param password
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/validatePass", method = RequestMethod.POST)
	@ResponseBody
	public Map validatePass(
			@RequestParam(value = "memberId", required = true) Long memberId,
			@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "name", required = true) String password,
			HttpServletRequest request) {

			Map<String, Object> map = new HashMap<String, Object>();
			String newPass = MD5Tool.MD5(param);
			HRTMember member = this.hrtMemberService.getMemberByid(memberId);
			if(member.getPassword().equals(newPass)){
				map.put("status", "y");
				map.put("info", "密码验证通过");
			}
			else{
				map.put("status", "n");
				map.put("info", "密码输入有误");
			}
			
		
		return map;
	}
	/**
	 * 修改手机号码
	 * @param memberId
	 * @param mobile
	 * @param code
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateMobile", method = RequestMethod.POST)
	@ResponseBody
	public Map updateMobile(
			@RequestParam(value = "memberId", required = true) Long memberId,
			@RequestParam(value = "mobile", required = true) String mobile,
			@RequestParam(value = "code", required = true) String code,
			HttpServletRequest request) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		HttpSession session = request.getSession();
		Integer flag = null;
		SmsRecord smsRecord = smsRecordService.getCodeByMobile(mobile);
		String newCode = smsRecord.getVaildCode();
		HRTMember member = this.hrtMemberService.getMemberByid(memberId);
		if(code.equals(newCode)){
			member.setMobile(mobile);
			hrtMemberService.updateHRTMember(member);
			session.setAttribute("HRTMember", member);
			smsRecord.setIsinvalid(1);
			try {
				smsRecordService.updateSmsRecord(smsRecord);
			} catch (Exception e) {
				e.printStackTrace();
			}
			map.put("mobile", mobile);
			flag = 0;
		}
		else{
			flag = 1;
		}
		
		map.put("flag", flag);
		return map;
	}
	
	/**
	 * 修改邮箱
	 * @param memberId
	 * @param mobile
	 * @param code
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateMail", method = RequestMethod.POST)
	@ResponseBody
	public Map updateMail(
			@RequestParam(value = "memberId", required = true) Long memberId,
			@RequestParam(value = "mail", required = true) String mail,
			@RequestParam(value = "code", required = true) String code,
			HttpServletRequest request) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		//HttpSession session = request.getSession();
		Integer flag = null;
		MailRecord mailRecord = mailRecordService.getCodeByMail(mail);
		String newCode = mailRecord.getVaildCode();
		HRTMember member = this.hrtMemberService.getMemberByid(memberId);
		if(code.equals(newCode)){
			member.setMail(mail);
			hrtMemberService.updateHRTMember(member);
			//session.setAttribute("HRTMember", member);
			mailRecord.setIsinvalid(1);
			try {
				mailRecordService.updateMailRecord(mailRecord);
			} catch (Exception e) {
				e.printStackTrace();
			}
			map.put("mail", mail);
			flag = 0;
		}
		else{
			flag = 1;
		}
		
		map.put("flag", flag);
		return map;
	}


}
